<script>
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";

export default {
  name: "ScrollTrigger",
  mounted() {
    gsap.registerPlugin(ScrollTrigger);
    const slides = gsap.utils.toArray(".slide");
    const activeSlideImages = gsap.utils.toArray(".active-slide img");

    function getInitialTranslateZ(slide) {
      const style = window.getComputedStyle(slide);
      const matrix = style.transform.match(/matrix3d\((.+)\)/);
      if (matrix) {
        const values = matrix[1].split(", ");
        return parseFloat(values[14] || 0);
      }
      return 0;
    }

    function mapRange(value, inMin, inMax, outMin, outMax) {
      return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
    }

    slides.forEach((slide, index) => {
      const initialZ = getInitialTranslateZ(slide);

      ScrollTrigger.create({
        trigger: ".container",
        start: "top top",
        end: "bottom bottom",
        scrub: true,
        onUpdate: (self) => {
          const progress = self.progress;
          const zIncrement = progress * 22500;
          const currentZ = initialZ + zIncrement;

          let opacity;

          if (currentZ > -2500) {
            opacity = mapRange(currentZ, -2500, 0, 0.5, 1);
          } else {
            opacity = mapRange(currentZ, -5000, -2500, 0, 0.5);
          }

          slide.style.opacity = opacity;
          slide.style.transform = `translateX(-50%) translateY(-50%) translateZ(${currentZ}px)`;

          if (currentZ < 100) {
            gsap.to(activeSlideImages[index], {
              opacity: 1,
              ease: "power3.out",
              duration: 1.5,
            });
          } else {
            gsap.to(activeSlideImages[index], {
              opacity: 0,
              ease: "power3.out",
              duration: 1.5,
            });
          }
        },
      });
    });
  },
};
</script>

<template>
  <div class="box">
    <nav>
      <div class="links-1">
        <a href="#">Works</a>
        <a href="#">Archive</a>
      </div>
      <div class="logo">
        <a href="#">Works</a>
      </div>
      <div class="links-2">
        <a href="#">Info</a>
        <a href="#">Contact</a>
      </div>
    </nav>

    <footer>
      <p>Watch Showreel</p>
      <p>Coming Soon</p>
    </footer>

    <div class="container">
      <div class="active-slide">
        <img src="../assets/ScrollTrigger/1.png" alt="" />
        <img src="../assets/ScrollTrigger/2.png" alt="" />
        <img src="../assets/ScrollTrigger/3.png" alt="" />
        <img src="../assets/ScrollTrigger/4.png" alt="" />
        <img src="../assets/ScrollTrigger/5.png" alt="" />
        <img src="../assets/ScrollTrigger/6.png" alt="" />
        <img src="../assets/ScrollTrigger/7.png" alt="" />
        <img src="../assets/ScrollTrigger/8.png" alt="" />
        <img src="../assets/ScrollTrigger/9.png" alt="" />
        <img src="../assets/ScrollTrigger/10.png" alt="" />
      </div>

      <div class="slider">
        <div class="slide" id="slide-1">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/1.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-2">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/2.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-3">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/3.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-4">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/4.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-5">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/5.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-6">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/6.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-7">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/7.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-8">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/8.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-9">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/9.png" alt="" />
          </div>
        </div>
        <div class="slide" id="slide-10">
          <div class="slide-copy">
            <p>Neo Elegance</p>
            <p id="index">( ES 9342 084F )</p>
          </div>
          <div class="slide-img">
            <img src="../assets/ScrollTrigger/10.png" alt="" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
@fontColor: rgb(230, 170, 40);

.box {
  background: #000;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

a,
p {
  color: @fontColor;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 12px;
}

nav {
  position: fixed;
  top: 0;
  width: 100%;
  padding: 1.5em 2em;
  display: flex;
  align-items: center;
}

footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 1.5em 2em;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

nav > div {
  flex: 1;
}

.links-1 {
  display: flex;
  gap: 2em;
}

.links-2 {
  display: flex;
  gap: 2em;
  justify-content: flex-end;
}

.logo {
  display: flex;
  justify-content: center;
}

.logo a {
  font-size: 16px;
  font-weight: bolder;
  letter-spacing: -0.02em;
}

.container {
  width: 100%;
  height: 2000vh;
}

.active-slide {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  opacity: 0.35;
  z-index: -1;
}

.active-slide img {
  position: absolute;
  filter: blur(50px);
  transform: scale(1.125);
}

.slider {
  position: fixed;
  top: 0;
  width: 100vw;
  height: 100vh;
  transform-style: preserve-3d;
  perspective: 750px;
  overflow: hidden;
}

.slide {
  position: absolute;
  width: 400px;
  height: 500px;
  overflow: hidden;
}

.slide-copy p {
  text-transform: uppercase;
  text-align: center;
  font-size: 13px;
  font-weight: bolder;
  line-height: 150%;
  color: @fontColor;
}

.slide-copy p#index {
  font-size: 11px;
  font-weight: 400;
  margin-bottom: 0.75em;
}

#slide-1 {
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translateX(-50%) translateY(-50%) translateZ(-22500px);
  opacity: 0;
}

#slide-2 {
  position: absolute;
  top: 50%;
  left: 30%;
  transform: translateX(-50%) translateY(-50%) translateZ(-20000px);
  opacity: 0;
}

#slide-3 {
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translateX(-50%) translateY(-50%) translateZ(-17500px);
  opacity: 0;
}

#slide-4 {
  position: absolute;
  top: 50%;
  left: 30%;
  transform: translateX(-50%) translateY(-50%) translateZ(-15000px);
  opacity: 0;
}

#slide-5 {
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translateX(-50%) translateY(-50%) translateZ(-12500px);
  opacity: 0;
}

#slide-6 {
  position: absolute;
  top: 50%;
  left: 30%;
  transform: translateX(-50%) translateY(-50%) translateZ(-10000px);
  opacity: 0;
}

#slide-7 {
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translateX(-50%) translateY(-50%) translateZ(-7500px);
  opacity: 0;
}

#slide-8 {
  position: absolute;
  top: 50%;
  left: 30%;
  transform: translateX(-50%) translateY(-50%) translateZ(-5000px);
  opacity: 0;
}

#slide-9 {
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translateX(-50%) translateY(-50%) translateZ(-2500px);
  opacity: 0.5;
}

#slide-10 {
  position: absolute;
  top: 50%;
  left: 30%;
  transform: translateX(-50%) translateY(-50%) translateZ(0px);
  opacity: 1;
}
</style>
